/**
 * Copyright 2010 Xeus Technologies 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 */

package org.xeustechnologies.jcksum.algo;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.xeustechnologies.jcksum.Checksum;

public class DigestChecksum implements Checksum {
    private final MessageDigest digest;

    public DigestChecksum(String name) throws NoSuchAlgorithmException {
        digest = MessageDigest.getInstance( name );
    }

    public long getValue() {
        byte[] chksum = digest.digest();
        BigInteger bigInt = new BigInteger( 1, chksum );

        return bigInt.longValue();
    }

    public void reset() {
        digest.reset();
    }

    public void update(int b) {
        digest.update( (byte) b );

    }

    public void update(byte[] b, int off, int len) {
        digest.update( b, 0, len );
    }

    public String getStringValue() {
        byte[] chksum = digest.digest();
        BigInteger bigInt = new BigInteger( 1, chksum );
        String output = bigInt.toString( 16 );

        return output;
    }

    public byte[] getBytes() {
        return digest.digest();
    }
}
